package com.sia.springcloud.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sia.springcloud.mapper.PointMapper;
import com.sia.springcloud.model.Point;
import com.sia.springcloud.protocol.model.BoschPoint;
import com.sia.springcloud.service.IPointService;
import com.sia.springcloud.result.GridDataResult;
import com.sia.springcloud.result.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Sirius
 * @Date: 2021/12/21 1:20
 * @Description: 通信协议点位服务层接口实现类
 * 主要提供的接口：添加点位、分页查询点位、批量删除点位、设置点位数采状态
 */
@Slf4j
@Service("PointService")
public class PointService extends ServiceImpl<PointMapper, Point> implements IPointService {

    @Resource
    private PointMapper pointMapper;

    /**
     * 添加点位
     *
     * @param point
     * @return
     */
    public String insertPoint(Point point) {
        log.info("插入点位：{}", point.toString());

        //查询数据库中是否有相同设备id，相同点位名的设备，如果有则不能插入
        QueryWrapper<Point> pointQueryWrapper = new QueryWrapper<>();
        pointQueryWrapper.eq("device_id", point.getDeviceId());
        pointQueryWrapper.eq("point_name", point.getPointName());
        Integer selectCount = pointMapper.selectCount(pointQueryWrapper);
        if (selectCount > 0) {
            return ResultUtil.createSimpleFailureJson("该变量名称已存在");
        }

        pointMapper.insert(point);

        return ResultUtil.createSimpleSuccessJson("变量添加成功");
    }

    /**
     * 根据设备 分页查询点位
     *
     * @param start
     * @param limit
     * @param deviceId
     * @return
     */
    public String queryPoint(Integer start, Integer limit, String deviceId) {


        QueryWrapper<Point> pointQueryWrapper = new QueryWrapper<>();
        pointQueryWrapper.eq("device_id", deviceId);
        Integer total = pointMapper.selectCount(pointQueryWrapper);

        Page<Point> pointPage = new Page<>();
        pointPage.setTotal(total);
        pointPage.setCurrent(start);
        pointPage.setSize(limit);


        IPage<Point> pointIPage = pointMapper.selectPage(pointPage, pointQueryWrapper);
        List<Point> pointList = pointIPage.getRecords();

        ArrayList<BoschPoint> boschPointList = new ArrayList<>();
        pointList.forEach(point -> {
            boschPointList.add(BoschPoint.reconvert(point));
        });


        GridDataResult<BoschPoint> gridDataResult = new GridDataResult<>();
        gridDataResult.setTotal(total);
        gridDataResult.setRoot(boschPointList);

        return JSON.toJSONString(gridDataResult);
    }


    /**
     * 删除点位
     *
     * @param pointIdList
     * @return
     */
    public String deletePoint(String[] pointIdList) {
        for (int i = 0; i < pointIdList.length; i++) {
            pointMapper.deleteById(Integer.parseInt(pointIdList[i]));
        }
        return ResultUtil.createSimpleSuccessJson("点位信息删除成功");
    }

    /**
     * 修改点位
     *
     * @param point
     * @return
     */
    public String updatePoint(Point point) {

        QueryWrapper<Point> pointQueryWrapper = new QueryWrapper<>();
        pointQueryWrapper.eq("device_id", point.getDeviceId());
        pointQueryWrapper.eq("point_name", point.getPointName());
        pointQueryWrapper.ne("id",point.getId());

        Integer selectCount = pointMapper.selectCount(pointQueryWrapper);
        if (selectCount > 0) {
            return ResultUtil.createSimpleFailureJson("该变量名称已存在");
        }
        pointMapper.updateById(point);
        return ResultUtil.createSimpleSuccessJson("变量修改成功");
    }


    /**
     * 获取点位表格格式
     */
    public String tableStructure() {


        // 1.pointName属性
        Map<String,String> pointNameMap1 = new HashMap<>();
        pointNameMap1.put("message", "不允许为空");
        pointNameMap1.put("required", "true");
        Map<String,String> pointNameMap2 = new HashMap<>();
        pointNameMap2.put("pattern", "^[a-z0-9A-Z]+$");
        pointNameMap2.put("type", "string");
        pointNameMap2.put("message", "只能为字母或字母+数字组合");
        List<Map> pointNameValidate = new ArrayList<>();
        pointNameValidate.add(pointNameMap1);
        pointNameValidate.add(pointNameMap2);


        HashMap<String,Object> pointName = new HashMap<>();
        pointName.put("prop", "pointName");
        pointName.put("label", "点位名称");
        pointName.put("type", "Input");
        pointName.put("validate", pointNameValidate);


        // 2.frequency属性
        Map<String,String> frequencyMap1 = new HashMap<>();
        frequencyMap1.put("message", "不允许为空");
        frequencyMap1.put("required", "true");
        Map<String,String> frequencyMap2 = new HashMap<>();
        frequencyMap2.put("pattern", "^(([0-9])|([1-9]+[0-9]*))$");
        frequencyMap2.put("type", "string");
        frequencyMap2.put("message", "必须为整数");
        List<Map> frequencyValidate = new ArrayList<>();
        frequencyValidate.add(frequencyMap1);
        frequencyValidate.add(frequencyMap2);


        HashMap<String,Object> frequency = new HashMap<>();
        frequency.put("prop", "frequency");
        frequency.put("label", "频率");
        frequency.put("type", "Input");
        frequency.put("validate", frequencyValidate);

        // 3.metricName属性
        Map<String, String> metricName1 = new HashMap<>();
        metricName1.put("message", "不允许为空");
        metricName1.put("required", "true");
        List<Map> metricNameValidate = new ArrayList<>();
        metricNameValidate.add(metricName1);


        HashMap<String, Object> metricName = new HashMap<>();
        metricName.put("prop", "metricName");
        metricName.put("label", "metricName");
        metricName.put("type", "Input");
        metricName.put("validate", metricNameValidate);

        // 4.target属性
        Map<String, String> target1 = new HashMap<>();
        target1.put("message", "不允许为空");
        target1.put("required", "true");
        List<Map> targetValidate = new ArrayList<>();
        targetValidate.add(target1);


        HashMap<String, Object> target = new HashMap<>();
        target.put("prop", "target");
        target.put("label", "target");
        target.put("type", "Input");
        target.put("validate", targetValidate);



        // 5.domain属性
        Map<String, String> domain1 = new HashMap<>();
        domain1.put("message", "不允许为空");
        domain1.put("required", "true");
        List<Map> domainValidate = new ArrayList<>();
        domainValidate.add(domain1);


        HashMap<String, Object> domain = new HashMap<>();
        domain.put("prop", "domain");
        domain.put("label", "domain");
        domain.put("type", "Input");
        domain.put("validate", domainValidate);

        // 6.varKey属性
        Map<String, String> varKey1 = new HashMap<>();
        varKey1.put("message", "不允许为空");
        varKey1.put("required", "true");
        List<Map> varKeyValidate = new ArrayList<>();
        varKeyValidate.add(varKey1);


        HashMap<String, Object> varKey = new HashMap<>();
        varKey.put("prop", "varKey");
        varKey.put("label", "变量标识");
        varKey.put("type", "Input");
        varKey.put("validate", varKeyValidate);



        //equipmentForm：把上面的属性信息包装到一个map中
        List<Map> tableData = new ArrayList<>();
        tableData.add(pointName);
        tableData.add(frequency);
        tableData.add(metricName);
        tableData.add(target);
        tableData.add(domain);
        tableData.add(varKey);


        Map<String,String> tableMap1 = new HashMap<>();
        tableMap1.put("width", "60");
        tableMap1.put("type", "selection");
        tableMap1.put("align", "center");
        Map<String,String> tableMap2 = new HashMap<>();
        tableMap2.put("title", "点位名称");
        tableMap2.put("align", "center");
        tableMap2.put("key", "pointName");
        Map<String,String> tableMap3 = new HashMap<>();
        tableMap3.put("title", "metricName");
        tableMap3.put("align", "center");
        tableMap3.put("key", "metricName");
        Map<String,String> tableMap4 = new HashMap<>();
        tableMap4.put("title", "target");
        tableMap4.put("align", "center");
        tableMap4.put("key", "target");
        Map<String,String> tableMap5 = new HashMap<>();
        tableMap5.put("title", "domain");
        tableMap5.put("align", "center");
        tableMap5.put("key", "domain");
        Map<String,String> tableMap6 = new HashMap<>();
        tableMap6.put("title", "varKey");
        tableMap6.put("align", "center");
        tableMap6.put("key", "varKey");
        List<Map> tableStyle = new ArrayList<>();
        tableStyle.add(tableMap1);
        tableStyle.add(tableMap2);
        tableStyle.add(tableMap3);
        tableStyle.add(tableMap4);
        tableStyle.add(tableMap5);
        tableStyle.add(tableMap6);


        Map<String,List> resMap = new HashMap<>();
        resMap.put("tableData", tableData);
        resMap.put("tableStyle", tableStyle);


        log.info("点位表单格式：{}",JSON.toJSONString(resMap));
        return JSON.toJSONString(resMap);

    }


}
